﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstaSharp.Model.Responses;

namespace InstaSharp.Endpoints.Users {
    public class Unauthenticated : InstagramAPI {
        
        public Unauthenticated (InstagramConfig config) : base(config, "/users/")  { }

        public UserResponse Get(int userId) {
            return (UserResponse)Mapper.Map<UserResponse>(GetJson(userId));
        }

        public string GetJson(int userId) {
            string uri = string.Format(base.Uri + "{0}/{1}", userId, AuthData);
            return HttpClient.GET(uri);
        }
        

        public MediasResponse Recent(string userId)
        {
            var user = Search(userId, 1);
            if (user == null || user.Data==null || user.Data.Count == 0) return null;
            return (MediasResponse)Mapper.Map<MediasResponse>(RecentJson(int.Parse(user.Data[0].Id)));
        }

        public MediasResponse Recent(long userId)
        {
            return (MediasResponse)Mapper.Map<MediasResponse>(RecentJson(userId));
        }

        private string RecentJson(long userId)
        {
            //https://api.instagram.com/v1/users/{user-id}/media/recent/?client_id=YOUR-CLIENT_ID
            string uri = string.Format(
            base.Uri + "{0}/media/recent/?client_id={1}", userId, InstagramConfig.ClientId);

            return HttpClient.GET(uri);
        }

        public UsersResponse Search(string searchTerm, int? count) {
            return (UsersResponse)Mapper.Map<UsersResponse>(SearchJson(searchTerm, count));
        }

        public string SearchJson(string searchTerm, int? count) {
            string uri = string.Format(base.Uri + "search{1}&q={0}", searchTerm, AuthData);
            
            if (count != null) uri += "&count=" + count;

            return HttpClient.GET(uri);
        }
    }
}
